﻿DONVI = function () {

}
var rows = 20;
var numberPage = 1;
DONVI.columns = new Array("Mã Đơn Vị", "Tên Đơn Vị", "Mã Tuyến Phát", "Bill Bắt Đầu", "Bill Kết Thúc");
DONVI.fieldText = new Array("MaDonVi", "TenDonVi", "BillBatDau", "BillKetThuc");
DONVI.MaDonVi = -1;
DONVI.isInsert = 0;
DONVI.arrays = new Array();
DONVI.Init = function () {
    DONVI.arrays = new Array();
    var obj;
    obj = new CheckField("TenDonVi", 1, "ErrTenDonVi");
    DONVI.Add(obj);
    obj = new CheckField("BillBatDau", 0, "ErrBillBatDau");
    DONVI.Add(obj);
    obj = new CheckField("BillKetThuc", 0, "ErrBillKetThuc");
    DONVI.Add(obj);
}

DONVI.Add = function (obj) {
    if (DONVI.arrays.contains(obj) == false) {
        DONVI.arrays.push(obj);
    }
}

DONVI.Insert = function () {
    var serviceUrl = Constants.service + "insertTblDonVis";
    if (Constants.OnSubmit(DONVI.arrays) == false) {
        return;
    }
    var data = { MaDonVi: $("#MaDonVi").val(),
        TenDonVi: $("#TenDonVi").val(),
        MaTuyenPhat: $("#MaTuyenPhat").val(),
        BillBatDau: $("#BillBatDau").val(),
        BillKetThuc: $("#BillKetThuc").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            DONVI.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}


DONVI.Delete = function (id) {    
    var data = {
        MaDonVi : id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblDonVis";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            DONVI.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

DONVI.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblDonVis";
    if (Constants.OnSubmit(DONVI.arrays) == false) {
        return;
    }
    var data = { MaDonVi: $("#MaDonVi").val(),
        TenDonVi: $("#TenDonVi").val(),
        MaTuyenPhat: $("#MaTuyenPhat").val(),
        BillBatDau: $("#BillBatDau").val(),
        BillKetThuc: $("#BillKetThuc").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {            
            DONVI.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });

}

DONVI.CreateTable = function (r, nbpage, data) {
    var myTable = DONVI.TitleColumns(DONVI.columns);
    myTable += DONVI.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_content").show();
    $("#div_list").children().remove();
    $("#div_list").append(myTable);    
}

DONVI.GetData = function () {
    var serviceUrl = Constants.service + "getAllTblDonVis";
    DONVI.MaDonVi = -1;
    DONVI.isInsert = 0;
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //  data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblDonVisResult;
            var dataTables = results;
            DONVI.CreateTable(rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

DONVI.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

DONVI.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaDonVi;
        var url = Constants.Url + "admin/Edit.aspx?pro=TblDonVi&ID1=" + id;
        myDataColumns += "<tr onclick='return DONVI.OnClick(" + id + ")' id=\"" + id + "\">";        
        myDataColumns += "<td>" + data[i].MaDonVi + "</td>"
        myDataColumns += "<td>" + data[i].TenDonVi + "</td>"
        myDataColumns += "<td>" + data[i].TenTuyenPhat + "</td>"
        myDataColumns += "<td>" + data[i].BillBatDau + "</td>"
        myDataColumns += "<td>" + data[i].BillKetThuc + "</td>"      
        myDataColumns += "<td><img onclick='return DONVI.EditItem("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return DONVI.Delete("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}

DONVI.OnClick = function (id) {
    DONVI.MaDonVi = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");    
}

DONVI.OnClickEdit = function () {
    if (DONVI.MaDonVi == -1) {
        alert(Constants.selectEdit);
    }
    else
        DONVI.EditItem(DONVI.MaDonVi);
}

DONVI.OnClickDelete = function () {
    if (DONVI.MaDonVi == -1) {
        alert(Constants.selectDelete);
    }
    else
        DONVI.Delete(DONVI.MaDonVi);
}

DONVI.EditItem = function (id) {
    var temp_item = "Modules/donvi/Edit";
  
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            Constants.GetTuyenPhat("MaTuyenPhat");
            DONVI.GetItem(id);
        }
    });
}



DONVI.GetItem = function (id) {
    $("#MaDonVi").val(id);
    var serviceUrl = Constants.service + "getItemByIdTblDonVi";
    var data = { MaDonVi: $("#MaDonVi").val() };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblDonViResult;
            if (result != null && result != undefined) {
                $("#MaDonVi").val(result.MaDonVi);
                $("#TenDonVi").val(result.TenDonVi);
                $("#MaTuyenPhat").val(result.MaTuyenPhat);
                $("#BillBatDau").val(result.BillBatDau);
                $("#BillKetThuc").val(result.BillKetThuc);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

DONVI.Cancel = function () {
    Constants.clearAll(DONVI.fieldText);
}

DONVI.AddNewItem = function () {
    var temp_item = "Modules/donvi/AddNew";
    $("#dialog").empty();
    DONVI.isInsert = 1;
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            Constants.GetTuyenPhat("MaTuyenPhat");
            DONVI.getMaDonVi();
        }
    });
}

DONVI.Enter = function () {
    $("#MaDonVi").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#TenDonVi").focus();
        }
    });
    $("#TenDonVi").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#BillBatDau").focus();
        }
    });
    $("#BillBatDau").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#BillKetThuc").focus();
        }
        var charCode = (event.which) ? event.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    });
    $("#BillKetThuc").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            if (DONVI.isInsert == 1) {
                DONVI.Insert();
            }
            else {
                DONVI.UpdateItem();
            }
        }
        var charCode = (event.which) ? event.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    });
}

DONVI.getMaDonVi = function () {
    var serviceUrl = Constants.service + "getMaTblDonVi";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblDonViResult;
            if (result != undefined && result != null) {
                $("#MaDonVi").val(result);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

DONVI.Search = function () {
    var serviceUrl = Constants.service + "getFindTblDonVis";
    var data = {
        value: $("#search").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl,
        data: json,
        contentType: "application/json",
        dataType: "json",
        cache: false,
        success: function (msg) {
            var results = msg.getFindTblDonVisResult;
            if (results != null && results.length > 0) {
                DONVI.CreateTable(Constants.rows, numberPage, results);
            }
            else {
                $("#div_list").children().remove();
                $("#div_list").html(Constants.find);
            }
        }
    });
}
$(document).ready(function () {
    DONVI.GetData();
    DONVI.Init();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            DONVI.Search();
        }
    });
    $("#search_on").click(function (event) {
        event.preventDefault();
        DONVI.Search();
    });
});